﻿/*
	Copyright 2008 CATsoft Development GmbH

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/
/*
	Edit task details
*/
var currentTask;
var usWeek = 0;
var calElement = null;
var calendar = null;
/***********************************************************************
 On Open
***********************************************************************/
function edittask_onOpen(){
	if( options.getValue("usweek") == "0" )
		usWeek = 0
	else
		usWeek = 1;
	
	RTM = new rtm();
  COMM = new comm();

	RTM.setToken( detailsViewData.getValue( "token" ) );
	if(COMM.connected){
		edittask_onOpen2();
	} else {
		COMM.testConnection(edittask_onOpen2);
	}
}

function edittask_onOpen2(){
	RTM.getLists( );
	showLists();

	if(COMM.connected){
		edittask_onOpen3();
	} else {
		COMM.testConnection(edittask_onOpen3);
	}
}

function edittask_onOpen3(){
	var taskId = detailsViewData.getValue("taskid");
	var i;

	RTM.getLocations();
	showLocations( locations );

	currentTask = detailsViewData.getValue("currenttask");
	if( currentTask > -1 )
	{
		taskName.value = detailsViewData.getValue("taskname");
		i = findEntry( taskList, detailsViewData.getValue("tasklist"));
		if( i > -1 )
			taskList.selectedIndex = i;
		i = findEntry( taskPrio, detailsViewData.getValue("taskprio"));
		if( i > -1 )
			taskPrio.selectedIndex = i;
		taskDue.value  =  detailsViewData.getValue("taskdue");
		taskRepeat.value  =  detailsViewData.getValue("taskrepeat");
		taskEst.value  =  detailsViewData.getValue("taskestimate");
		taskTags.value  =  detailsViewData.getValue("tasktags");
		i = findEntry( taskLocation, detailsViewData.getValue("tasklocation"));
		if( i > -1 )
			taskLocation.selectedIndex = i;
		taskURL.value  =  detailsViewData.getValue("taskurl");
	}
	else
	{		// Set Default List
		for( var j = 0; j < lists.length; j++)
			if( lists[j].position == "-1")
			{
				taskList.selectedIndex = j;
				break;
			}			
	}
}

/***********************************************************************
 Edit Task OK
***********************************************************************/
function editTaskOK() {
	
	detailsViewData.putValue("currenttask", currentTask );
	detailsViewData.putValue("taskname", taskName.value );
	detailsViewData.putValue("tasklist", getSelected( taskList ));
	detailsViewData.putValue("taskpriority", getSelected( taskPrio));
	detailsViewData.putValue("taskdue", taskDue.value ); 
	detailsViewData.putValue("taskrepeat",taskRepeat.value);
	detailsViewData.putValue("taskestimate",taskEst.value);
	detailsViewData.putValue("tasktags",taskTags.value);
	detailsViewData.putValue("tasklocation", getSelected( taskLocation));
	detailsViewData.putValue("taskurl",taskURL.value);
	detailsViewData.putValue("saveTask", 1 );

	var closeDetailsView = detailsViewData.getValue("closeDetailsView");
  closeDetailsView();  
}

/***********************************************************************
 Edit Task Cancel
***********************************************************************/
function editTaskCancel() {
	var closeDetailsView = detailsViewData.getValue("closeDetailsView");
  closeDetailsView();  
}

/***********************************************************************
 Edit Task Cancel
***********************************************************************/
function showLocations( locations ){
	for( var i = 0; i < locations.length; i++){
		entry = "<item height=\"20\" name=\"" + locations[i].id + "\">"
		entry = entry + " <label x=\"0\" font=\"tahoma\" size=\"8\">" + locations[i].name + "</label></item>"
		taskLocation.AppendElement(entry);
	}
}

/***********************************************************************
	Find index of an entry in combobox
***********************************************************************/
function findEntry( c, entry ){
	var ret = -1;

	for( var i = 0;i < c.children.count; i++)
		if( c.children.item(i).name == entry )
		{
			ret = i;
			break;
		}
	return ret;
}

/***********************************************************************
 Get Selected Name in combobox
***********************************************************************/
function getSelected( c ){
	var item = c.selectedItem;
	if( item != null )
		return item.name
	else
		return "";
}

/***********************************************************************
	Show lists
***********************************************************************/
function showLists(){

	for(var i = 0; i < lists.length; ++i) {
		var listItem = lists[i];
		if( listItem.filter == '' )
		{
			entry = "<item height=\"25\" name=\"" + listItem.id + "\">"
			entry = entry + " <label x=\"0\" font=\"tahoma\" size=\"8\">" + listItem.name + "</label></item>"
			taskList.AppendElement(entry);
		}

	}
}

/***********************************************************************
	Get Month Days
***********************************************************************/
function getDays(month, year) {
var ar = new Array(12)
ar[0] = 31 // January
ar[1] = (leapYear(year)) ? 29 : 28 // February
ar[2] = 31 // March
ar[3] = 30 // April
ar[4] = 31 // May
ar[5] = 30 // June
ar[6] = 31 // July
ar[7] = 31 // August
ar[8] = 30 // September
ar[9] = 31 // October
ar[10] = 30 // November
ar[11] = 31 // December
return ar[month]
}

/***********************************************************************
	Get Leap Year
***********************************************************************/
function leapYear(year) {
if (year % 4 == 0) // basic rule		-> Wrong in 2100
	return true // is leap year
else
	return false // is not leap year
}

/***********************************************************************
	Get Month Name
***********************************************************************/
function getMonthName(month) {
	var i = FREQ_MONTHNAMESx.indexOf( month );
	i = FREQ_MONTHNAMESx.indexOf( ":", i );
	var j = FREQ_MONTHNAMESx.indexOf( ";", i );
	return FREQ_MONTHNAMESx.substr( i + 1, j - i - 1);
}

/***********************************************************************
 Show Calendar	
***********************************************************************/
function showCalendar() {
var element = '';

	if(calElement != null)
	{
		div1.removeElement( calElement )
		calElement = null;
	}
	else
	{
		if( taskDue.value > '' )
			d = RTM.parseTime( taskDue.value );
		else
			d = new Date();
		element ="<div name=\"div2\" x=\"218\" y=\"60\" width=\"146\" height=\"142\" visible=\"true\" background=\"#AAAAAAAA\"/>"
		calElement = div1.appendElement( element );
		element = "<div name=\"calendardiv\" x=\"2\" y=\"2\" width=\"142\" height=\"138\" background=\"#FFFFFFFF\"/>"
		calendar = calElement.appendElement( element );
		
		showMonth(d.getYear(), d.getMonth(), d.getDate());
	}
}

/***********************************************************************
	Draw Month
***********************************************************************/
function showMonth( year, month, date ){
	if (year < 1000)
		year+=1900

	var monthName = getMonthName(month)
	var firstDayInstance = new Date(year, month, 1)
	var firstDay = firstDayInstance.getDay()
	var days = getDays(month, year)

	// call function to draw calendar
	var nextMonth, prevMonth;
	calendar.removeAllElements();
	if(month==0)
		prevMonth = new Date( parseInt( year ) - 1, 11, 1 )
	else
		prevMonth = new Date( year, parseInt( month ) - 1, 1 );

	if(month==11)
		nextMonth = new Date( parseInt( year ) + 1, 0, 1 )
	else
		nextMonth = new Date( year, parseInt( month ) + 1, 1 );

	drawHeader(monthName + " " + year, prevMonth, nextMonth);
	drawCal(firstDay + usWeek, days, date, month, year)
}

/***********************************************************************
	Draw Header
***********************************************************************/
function drawHeader( monthName, prevMonth, nextMonth, month, year ){
var	days, entry = "";

	if(usWeek==1)
		days = WEEKDAYS_US
	else
		days = WEEKDAYS_EU;

	entry = "<label height=\"16\" width=\"20\" x=\"0\" y=\"0\" align=\"center\" bold=\"true\" enabled=\"true\" font=\"tahoma\" size=\"8\" cursor=\"arrow\" onclick=\"showMonth('" + prevMonth.getYear() + "','" + prevMonth.getMonth() + "','1')\">&lt;</label>";
	calendar.AppendElement( entry )

	entry = "<label height=\"16\" width=\"20\" x=\"120\" y=\"0\" align=\"center\" bold=\"true\"  enabled=\"true\" font=\"tahoma\" size=\"8\" cursor=\"arrow\" onclick=\"showMonth('" + nextMonth.getYear() + "','" + nextMonth.getMonth() + "','1')\">&gt;</label>";
	calendar.AppendElement( entry )

	entry = "<label height=\"16\" width=\"100\" x=\"20\" y=\"0\" align=\"center\" bold=\"true\" font=\"tahoma\" size=\"8\">" + monthName + "</label>";
	calendar.AppendElement( entry )

	for( var i = 0; i < 7; i++)
	{
		entry = "<label height=\"16\" width=\"20\" x=\"" + i * 20 + "\" y=\"20\" align=\"right\" bold=\"true\" font=\"tahoma\" size=\"8\"";
		if( ((i==0) && (usWeek ==1)) || ((i==5) && (usWeek==0)) || (i==6))
			entry = entry + " color=\"#0000FF\" ";
		entry = entry + ">" + days.substr(i,1) + "</label>";
		calendar.AppendElement( entry )
	}
}

/***********************************************************************
	Draw Calendar
***********************************************************************/
function drawCal(firstDay, lastDate, date, cMonth, cYear) {
var digit = 1;
var curCell = 1;
var entry = "";
var d = new Date();
var y = d.getYear();
var m =d.getMonth();
var t = d.getDate();


	for (var row = 1; row <= Math.ceil((lastDate + firstDay - 1) / 7); ++row) {
		for (var col = 1; col <= 7; ++col) {
			if (digit > lastDate)
				break
			if (curCell < firstDay)
				curCell++
			else
			{
				entry = "<label height=\"16\" width=\"20\" x=\"" + (col - 1) * 20 + "\" y=\"" + (row + 1) * 20 + "\" align=\"right\" font=\"tahoma\" size=\"8\" enabled=\"true\" cursor=\"arrow\" onclick=\"setDate('" + cYear + "','" + cMonth + "','" + digit + "')\"";
				if ((digit == t) && (m == cMonth ) && (y == cYear))
					entry = entry + " bold=\"true\""
				if(((col==1)&&(usWeek==1)) || ((col==6)&&(usWeek==0)) || (col==7))
					entry = entry + " color=\"#0000FF\" ";
				entry = entry + ">" + digit;
				entry = entry + "</label>";
				digit++
				calendar.AppendElement( entry );
			}
		}
	}
}


/***********************************************************************
	Set Date
***********************************************************************/
function setDate( y, m, d ){
	taskDue.value = d + ". " + getMonthName( m ) + " " + y 
	div1.removeElement( calElement );
	calElement = null;
}